import * as cc from "cc";
import Loader from "../../Script/Common/Loader";
import yyComponent from "../../Script/Common/yyComponent";
import { EventType } from "../../Script/GameSpecial/GameEventType";

const { ccclass, property } = cc._decorator;
//技能按钮
@ccclass
export default class SkillBtn extends yyComponent {

    public init() {
        this.initCoolMask();
        this.initCoolState();
        this.initTouchState();

        this.onEvents();

    }
    protected onEvents() {
        this.node.on("touchstart", this.onTouchStart, this);
        this.node.on("touchend", this.onTouchEnd, this);
        this.node.on("touchcancel", this.onTouchEnd, this);
    }

    protected setData(skillImg) {
        //图片
        this.setImg(skillImg);

    }

    public reset() {
        this.resetCoolMask();
        this.resetCoolState();
        this.resetTouchState();
    }

    /********************************************************对外方法********************************************************/
    /**进入冷却状态 */
    public startCooling() {
        this.cooled = false;
        this.setCoolPrecess(0);
    }
    /**更新冷却状态 */
    public setCoolPrecess(rate) {
        if (rate > 1) rate = 1;
        if (rate < 0) rate = 0;
        //todo:设置冷却遮罩效果
        this.setCoolRate(rate);
        if (rate >= 1) {
            this.coolFinish();
        }
    }
    /**设置按钮代表的技能 */
    public setSkill(skillImg) {
        this.setData(skillImg);
    }
    /**按钮编号 */
    protected btnId;
    public setBtnId(id) {
        this.btnId = id;
    }

    /********************************************************管理对象********************************************************/
    //#region 技能图标
    @property(cc.Sprite)
    protected img: cc.Sprite = null;
    public setImg(imgName: string) {
        Loader.loadBundleRes("LevelUI", "Image/" + imgName, (res) => {
            this.img.spriteFrame = res;
            this.coolSprite.spriteFrame = res;
        }, cc.SpriteFrame, false);
    }
    //#endregion

    //#region 冷却遮罩
    @property(cc.Sprite)
    protected coolSprite: cc.Sprite = null;
    protected initCoolMask() {
        this.coolSprite.fillRange = 0;
    }
    protected resetCoolMask() {
        this.coolSprite.fillRange = 0;
    }
    protected setCoolRate(r) {
        this.coolSprite.fillRange = 1 - r;
    }
    //#endregion

    /********************************************************属性数据********************************************************/
    //#region 技能数据
    public skillId: number = 0;
    //#endregion

    //#region 冷却状态
    /**技能是否已冷却完毕 */
    protected cooled: boolean = true;
    protected initCoolState() {
        this.cooled = true;
    }
    protected resetCoolState() {
        this.cooled = true;
        this.setCoolPrecess(1);
    }

    protected coolFinish() {
        this.cooled = true;
    }
    //#endregion

    /********************************************************点击回调********************************************************/

    //#region 点击功能
    protected btnSprite:cc.Sprite = null;
    protected touched: boolean = false;
    protected normalColor: cc.Color = cc.Color.WHITE;
    protected darkColor: cc.Color = cc.color(200, 200, 200, 255);
    protected initTouchState() {
        this.touched = false;
        this.btnSprite = this.node.getComponent(cc.Sprite);
        this.btnSprite.color = this.normalColor;
    }
    protected resetTouchState() {
        this.touched = false;
        this.btnSprite.color = this.normalColor;
    }
    protected onTouchStart() {
        if (!this.cooled) return;
        if (this.touched) return;
        this.touched = true;
        this.emit(EventType.Common.CtrlEvent.btnTouchStart, this.btnId);
        this.btnSprite.color = this.darkColor;
    }
    protected onTouchEnd() {
        if (!this.touched) return;
        this.touched = false;
        this.emit(EventType.Common.CtrlEvent.btnTouchEnd, this.btnId);
        this.btnSprite.color = this.normalColor;
    }
    //#endregion
}
